#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())
rm(.Random.seed)

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")
R_DATA_PATH <- file.path(R_ROOT, "Donnees")
R_RESULTS_PATH <- file.path(R_ROOT, "Resultats")

R_STATIC_DIR <- file.path(R_DATA_PATH, "Bylot/Fixe")
R_MOBILE_DIR <- file.path(R_DATA_PATH, "Bylot/Mobile")


#'#
#'# Chargement des dependances
#'# 

#source(file.path(R_WORKING_DIRECTORY, "Utils/initCluster.R"))
source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))


#'#
#'# Chargement des donnees
#'# 

locsclasses <- c(3, 2, 1, 0, "A", "B")

#staticErrors <- loadData(R_STATIC_DIR, locsclasses=locsclasses)
staticData <- loadData(R_STATIC_DIR, raw=TRUE, static=TRUE)
mobileData <- loadData(R_MOBILE_DIR, raw=TRUE, static=FALSE)



colours <- list("argos"=c("3"="black", "2"="orange", "1"="blue", "0"="magenta", "A"="green3", "B"="brown", "Z"="purple"),
    "GPS" = "red")
labels <- list("argos"=c("3"="LC3", "2"="LC2", "1"="LC1", "0"="LC0", "A"="LCA", "B"="LCB", "Z"="LCZ"), "GPS"="GPS")
shapes <- list("argos"=16, "GPS"= 3)

lim <- 1500

#'#
#'# Cartes statiques
#'#
map.v11 <- static.map(staticData, "1", 1, 113049, "A) Static test - Hilltop - Replicate 1 - PTT 113049", colours, labels, shapes, lim)
print(map.v11)
map.v12 <- static.map(staticData, "1", 2, 113055, "B) Static test - Hilltop - Replicate 2 - PTT 113055", colours, labels, shapes, lim)
print(map.v12)

map.v21 <- static.map(staticData, "2", 1, 113046, "C) Static test - Moderate relief - Replicate 1 - PTT 113046", colours, labels, shapes, lim)
print(map.v21)
map.v22 <- static.map(staticData, "2", 2, 113047, "D) Static test - Moderate relief - Replicate 2 - PTT 113047", colours, labels, shapes, lim)
print(map.v22)

map.v31 <- static.map(staticData, "3", 1, 113048, "E) Static test - Incise valley - Replicate 1 - PTT 113048", colours, labels, shapes, lim)
print(map.v31)
map.v32 <- static.map(staticData, "3", 1, 113050, "F) Static test - Incise valley - Replicate 1 - PTT 113050", colours, labels, shapes, lim)
print(map.v32)



#'#
#'# Cartes mobiles
#'#

str(mobileData)
g1 <- mobileData[[17]]@gps
plot(g1$longitude, g1$latitude)


source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))

map.m1 <- mobile.map(mobileData, 1, 113052, "G) Mobile test - Loop - PTT 113052", colours, labels, shapes)
print(map.m1)
map.m2 <- mobile.map(mobileData, 13, 113055, "H) Mobile test - Straight - PTT 113055", colours, labels, shapes)
print(map.m2)
map.m3 <- mobile.map(mobileData, 14, 113053, "I) Mobile test - Loop - PTT 113053", colours, labels, shapes)
print(map.m3)
map.m4 <- mobile.map(mobileData, 15, 113052, "J) Mobile test - Straight - PTT 113052", colours, labels, shapes)
print(map.m4)
map.m5 <- mobile.map(mobileData, 17, 113054, "K) Mobile test - Loop - PTT 113054", colours, labels, shapes)
print(map.m5)
map.m6 <- mobile.map(mobileData, 19, 113051, "L) Mobile test - Loop - PTT 113051", colours, labels, shapes)
print(map.m6)


#'# Visibiliy 1
vis1 <- staticData[["1"]]
vis1.argos <- vis1@argos
vis1.gps <- vis1@gps
setkey(vis1.argos, session, id, LC)
setkey(vis1.gps, session)

vsess <- 1
ptt <- 113049

lim <- 1000

va11 <- vis1.argos[list(vsess, ptt)]
vg <- vis1.gps[list(vsess)]


xcenter <- mean(vg$longitude)
ycenter <- mean(vg$latitude)

xlim <- c(xcenter - lim, xcenter + lim)
ylim <- c(ycenter - lim, ycenter + lim)

va11[, round(sum(argLong > xlim[1] & argLong < xlim[2] & argLat > ylim[1] & argLat < ylim[2])/.N * 100,1)]

inc <- 


LCcol <- LCcolours[names(LCcolours) %in% unique(va11$LC)]
LClab <- LClabels[names(LClabels) %in% unique(va11$LC)]

colours <- c(LCcol, "gps"=GPScolour)
labels <- c(LClab, "gps" = GPSlabel)
shapes <- c(rep(LCshape, length(LCcol)), GPSshape)
names(shapes) <- c(names(LCcol), "gps")

vis11.plot <- ggplot(data=va11 , aes(x=argLong, y=argLat)) +
    geom_point()+
    xlab("Longitude")+
    ylab("Latitude")
vis11.plot <- vis11.plot + aes(colour=LC, shape=LC) + theme_bw()

vis11.plot <- vis11.plot + geom_point(data=vis1.gps[list(vsess)], 
    aes(x=longitude, y=latitude, colour="gps", shape="gps"))


vis11.plot <- vis11.plot + scale_colour_manual(name="Location type", values=colours, 
    labels = labels,
    breaks=c(names(LClab), "gps"))

vis11.plot <- vis11.plot + scale_shape_manual(name="Location type", labels = labels, values=shapes, breaks=c(names(LClab), "gps"))

print(vis11.plot)



state1 <- c(rep(c(rep("N",7), rep("Y",7)),2))
year <- rep(c(2003:2009), 4)
group1 <- c(rep("C",14), rep("E", 14))
group2 <- paste(state1,group1, sep = "")
beta <-c(0.16,0.15,0.08,0.08,0.18,0.48,0.14,0.19,0.00,0.00,0.04,0.08,0.27,0.03,0.11,0.12,0.09,0.09,0.10,0.19,0.16,0.00,0.11,0.07,0.08,0.09,0.19,0.10)
lcl  <-c(0.13,0.12,0.05,0.05,0.12,0.35,0.06,0.13,0.00,0.00,0.01,0.04,0.20,0.00,0.09,0.09,0.06,0.06,0.07,0.15,0.11,0.00,0.07,0.03,0.05,0.06,0.15,0.06)
ucl  <-c(0.20,0.20,0.13,0.14,0.27,0.61,0.28,0.27,0.00,1.00,0.16,0.16,0.36,0.82,0.14,0.15,0.13,0.13,0.15,0.23,0.21,0.00,0.15,0.14,0.12,0.12,0.23,0.16)
data <- data.frame(state1,year,group1,group2,beta,lcl,ucl)
library(ggplot2)
pd <- position_dodge(.65)
ggplot(data =data,aes(x= year, y = beta, colour = state1, group = group2, shape = group1)) +    
    geom_point(position = pd, size = 4) +
    geom_errorbar(aes(ymin = lcl, ymax = ucl),colour = "black", width = 0.5, position = pd) +
    scale_colour_hue(name="Treatment & State",  #Legend label, use darker colors
        labels=c("Control", "Exclosure"),
        l=40) + scale_shape(name="State", labels = c("Non-F", "Flwr"))